package com.ys.controller.web.storage;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.storage.TccLocationTransfer;
import com.ys.entity.storage.TccLocationTransferItem;
import com.ys.service.flow.YspTaskService;
import com.ys.service.storage.TccLocationTransferService;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.Flow;
import com.ys.vo.R;
import com.ys.vo.W;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 货位转移单
 */
@Controller
@RequestMapping("/web/storage/tccLocationTransfer")
public class WebTccLocationTransferController {
	private static final Logger logger = LoggerFactory.getLogger(WebTccLocationTransferController.class);

	@Autowired
	private CommonApiService<TccLocationTransfer> commonApiService;

    @Autowired
    private YspTaskService yspTaskService;

    @Autowired
    private TccLocationTransferService tccLocationTransferService;

    private final Long MENU_ID = MenuId.TCC_LocationTransfer;

    /*
     * 新增
     */
    @GetMapping("/toAdd")
    public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response) {
        try {
            Map<String, Object> extraParam = new HashMap<>();
            //关联数据
            extraParam.put("status", 1);

            TccLocationTransfer entity = new TccLocationTransfer();
            //关联数据
            String bid = commonApiService.getCommonNextBid(MENU_ID);
            entity.setBid(bid);
            entity.setCreateTime(new Date());

            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MENU_ID,new ArrayList<>());
            return commonApiService.toAdd(model, entity, MENU_ID, extraParam, subGridConfig);

        }catch (Exception e) {
            logger.error("新增页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }

    /*
     * 编辑
     */
    @GetMapping("/toEdit")
    public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response, String bid) {
        try {
            TccLocationTransfer tccLocationTransfer = new TccLocationTransfer().setBid(bid).queryFirst();
            if (tccLocationTransfer == null){
                return W.error(model, "货位转移单不存在");
            }
            Map<String, Object> extraParam = null;

            List<Map<String, Object>> itemList = new TccLocationTransferItem()
            .addLeftJoin("TCC_StockControl AA ON AA.id = A.stockControlBeforeId")
            .addLeftJoin("TCC_Location SD ON SD.Chw = AA.Chw")//货位信息表
                    .addField("SD.Vhw AS vhwYc")//货位
                    .addLeftJoin("TCC_WarehouseArea SE ON SE.Ckq = SD.Ckq")//库区
                    .addField("SE.Vkq AS vkqYc")//库区
                    .addLeftJoin("TCC_Warehouse SF ON SF.Cck = SE.Cck")//仓库
                    .addField("SF.Vck AS vckYc")//仓库
                    .addField("AA.productName AS productName")//商品名称
                    .addField("AA.productCode AS productCode")//商品编码
                    .addField("AA.productNum AS productNum")//商品可用数量
                    .addField("AA.productNumAll AS productNumAll")//商品总数量
                    .addField("AA.drksj AS drksj")//入库时间
                    .addField("AA.vgsbm AS vgsbm")//运营公司编码
                    .addField("AA.vgsjc AS vgsjc")//运营公司简称

                    .addLeftJoin("TCC_Location SDYR ON SDYR.Chw = A.Chw")//货位信息表
                    .addField("SDYR.Vhw AS vhwYr")//移入货位
                    .addLeftJoin("TCC_WarehouseArea SEYR ON SEYR.Ckq = SDYR.Ckq")//库区
                    .addField("SEYR.Vkq AS vkqYr")//移入库区
                    .addLeftJoin("TCC_Warehouse SFYR ON SFYR.Cck = SEYR.Cck")//仓库
                    .addField("SFYR.Vck AS vckYr")//移入仓库

                    .addLeftJoin("TP_ProductActive B on AA.productCode = B.Code")
                    .addField("B.CodeMerge codeMerge")

                    .addField("DATEDIFF(day, AA.Drksj, getdate()) kcdays")
                    .addLeftJoin("TB_CompanyInfo SH ON SH.VId = AA.companyVid")
                    .addField("SH.Vname AS companyName")//公司名称
                    .setPbid(tccLocationTransfer.getBid()).queryMap();
//
            // 子列表配置信息
            SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MENU_ID,itemList);

            return commonApiService.toEdit(model,tccLocationTransfer.getId(), new TccLocationTransfer(), MENU_ID, extraParam, subGridConfig);
        }catch (Exception e) {
            logger.error("编辑页面出错：",e.getMessage(),e);
            return W.error(model, e.getMessage());
        }
    }

}






